package lws.com.liwushuo.adapter.shouye;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import lws.com.liwushuo.R;

/**
 * Created by ZQ on 2016/10/17.
 */

public class HotSearchAdapter extends RecyclerView.Adapter<HotSearchAdapter.MyHolder> implements View.OnClickListener {

    private Context context;
    private List<String> list;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public HotSearchAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.shouye_search_hot_item, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (position < 3) {
            holder.hotText.setTextColor(Color.RED);
        }
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
        holder.hotText.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener!=null){
            int position = (int) v.getTag();
            String keyWord = list.get(position);
            onItemClickListener.onItemClick(v,position,keyWord);
        }
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        private TextView hotText;

        public MyHolder(View itemView) {
            super(itemView);

            hotText = (TextView) itemView.findViewById(R.id.shouye_search_hotText);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position, String keyWord);
    }

}
